package com.zhangzhan.gulimallware.config;


import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

import static com.zhangzhan.gulimallcommcon.constant.orderAndWareUnLockCons.WareUnLockConstant.*;


/**
 * @author 张一恒
 * @version 1.0
 * @description: 库存服务解锁库存mq ,spring把mq配置加入到ioc中会自动创建对应的队列或交换机也会自动绑定。只有一次效果，第二次重启服务不会覆盖相关交换机或队列之前配置。
 * @date 2024/7/2 11:28
 */
@Configuration
public class OrderWareUnlockMqConfig {
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 延时队列
     *
     * @return
     */
    @Bean
    public Queue wareCreateWare() {
        Map<String, Object> arguments = Map.of(X_DEAD_LETTER_EXCHANGE, WARE_EVENT_EXCHANGE,
                X_DEAD_LETTER_ROUTING_KEY, WARE_RELEASE_WARE,
                X_MESSAGE_TTL, ORDER_FAIL_TTL);
        return new Queue(WARE_DELAY_QUEUE, true, false, false, arguments);
    }


    /**
     * 接收死信的队列
     *
     * @return
     */
    @Bean
    public Queue wareReleaseWareQueue() {
        return new Queue(WARE_RELEASE_WARE_QUEUE, true, false, false);
    }

    /**
     * 处理和接收死信的交换机
     *
     * @return
     */
    @Bean
    public Exchange wareEventExchange() {
        return new TopicExchange(WARE_EVENT_EXCHANGE, true, false);
    }


    /**
     * 延时队列绑定交换机
     *
     * @return
     */
    @Bean
    public Binding eventExchangeBindingWareCreate() {
        return new Binding(WARE_DELAY_QUEUE, Binding.DestinationType.QUEUE, WARE_EVENT_EXCHANGE, WARE_UNLOCK_WARE, null);
    }

    /**
     * 死信队列绑定交换机
     *
     * @return
     */
    @Bean
    public Binding eventExchangeBindingWareRelease() {
        return new Binding(WARE_RELEASE_WARE_QUEUE, Binding.DestinationType.QUEUE, WARE_EVENT_EXCHANGE, WARE_RELEASE_WARE, null);
    }

}
